package com.fakeDYChat.common.user.dao;

import com.fakeDYChat.common.common.domain.enums.YesOrNoEnum;
import com.fakeDYChat.common.user.domain.entity.UserBackpack;
import com.fakeDYChat.common.user.domain.enums.IdempotentEnum;
import com.fakeDYChat.common.user.mapper.UserBackpackMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户背包表 服务实现类
 * </p>
 *
 * @author Tang
 * @since 2023-12-30
 */
@Service
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack> {

    /**
     * 获取对应物品的可用个数
     *
     * @param uid    用户id
     * @param itemId 物品id
     * @return java.lang.Integer
     * @author Tang
     * @date 2023/12/30 15:40
     **/
    public Integer selectCountByValidItemId(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }

    /**
     * 获取ItemId为itemId的最早获得的可用物品
     *
     * @param uid    用户id
     * @param itemId 物品id
     * @return com.fakeDYChat.common.user.domain.entity.UserBackpack
     * @author Tang
     * @date 2023/12/30 20:26
     **/
    public UserBackpack getFirstValidItem(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .orderByAsc(UserBackpack::getId)
                .last("limit 1")
                .one();
    }

    /**
     * 使用对应id的物品
     *
     * @param modifyCard 物品信息
     * @return java.lang.Boolean
     * @author Tang
     * @date 2023/12/30 20:31
     **/
    public Boolean useItem(UserBackpack modifyCard) {
        return lambdaUpdate()
                .eq(UserBackpack::getId, modifyCard.getId())
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .set(UserBackpack::getStatus, YesOrNoEnum.YES.getStatus())
                .update();
    }

    /**
     * 查询指定物品中用户拥有的物品
     *
     * @param uid     用户id
     * @param itemIds 物品id
     * @return void
     * @author Tang
     * @date 2024/1/2 19:16
     **/
    public List<UserBackpack> getUserOwnByItemIds(Long uid, List<Long> itemIds) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .in(UserBackpack::getItemId, itemIds)
                .list();
    }

    /**
     * 根据幂等号获取对应的物品
     *
     * @param idempotent 幂等号
     * @return com.fakeDYChat.common.user.domain.entity.UserBackpack
     * @author Tang
     * @date 2024/1/3 18:07
     **/
    public UserBackpack getByIdem(String idempotent) {
        return lambdaQuery()
                .eq(UserBackpack::getIdempotent, idempotent)
                .one();
    }
}
